(function () {
	/**
	 * 定义$代表jQuery对象
	 */
	var $ = jQuery;
	/**
	 * 坐席标识1,营业员标识0
	 */
	var jobCode = "1";
	/**
	 * 当前用户userid
	 */
	var userId = "";
	/**
	 * 当前用户userName
	 */
	var userName="";
	/**
	 * 当前用户对象
	 */
	var userInfoObj;
	/**
	 * 忙碌标识
	 */
	var busyCode = "1";
	/**
	 * 在线标识
	 */
	var onlineCode = "0";
	/**
	 * 闪烁效果
	 */
	var visitIsIn = "msgFlash1";
    var iFlash = setInterval(function () {
        if (visitIsIn === "msgFlash1") {
            $('.' + visitIsIn).removeClass(visitIsIn).addClass("msgFlash2");
            visitIsIn = "msgFlash2";
        } else {
            $('.' + visitIsIn).removeClass(visitIsIn).addClass("msgFlash1");
            visitIsIn = "msgFlash1";
        }

    }, 500);

    var timerArr;
    var title_text = $("title").text();
    $.extend({
    	/**
		 * 调用方法
		 * $.blinkTitle.show();
		 * $.blinkTitle.clear();
		 */
		blinkTitle : {
			show : function() {	//有新消息时在title处闪烁提示
				var step=0, _title = title_text;
	
				var timer = setInterval(function() {
					step++;
					if (step==3) {step=1};
					if (step==1) {document.title='【　　　】'+_title};
					if (step==2) {document.title='【新消息】'+_title};
				}, 500);
				timerArr = [timer, _title];
			},
			/**
			 * @param timerArr[0], timer标记
			 * @param timerArr[1], 初始的title文本内容
			 */
			clear : function() {	//去除闪烁提示，恢复初始title文本
				if(timerArr) {
					clearInterval(timerArr[0]);	
					document.title = timerArr[1];
				};
			}
		}
	});

    /**
     * ready
     */
    $(function(){
    	var CurrentActive;
    	$(window).focus(function() {
    	    //处于激活状态
    		$.blinkTitle.clear();
    		CurrentActive = false;
    	});
    	$(window).blur(function() {
    	    //处于未激活状态
    		CurrentActive = true;
    	});
	/**
	 * 连接大厅的socket
	 */
	var hallSocket = io.connect('http://' + _host + ':' + _port + '/hall');
	
	/**
	 * 连接大厅后，触发connChatRoom方法</br>
	 */
	var connectHandler = function() {
		/**
		 * 监听leaveInfo，验证重复登录
		 * 监听userList，首次进入监听，获取所有在线用户
		 * 监听addUserToList，坐席已在线，当有新用户进入时，刷新列表
		 */
		hallSocket.emit("connChatRoom", userId, jobCode);
		
		/**
		 * 加载客服常用回复
		 */
		hallSocket.emit("autoReply");
    };
    var replyClick = function(){
    	var $this = $(this);
    	var _visitId = $this.parent('div.hfls').attr("seatid");
    	var ue = UE.getEditor('editor_'+_visitId);
    	ue.setContent($this.attr("title"),true);
    };
    /**
     * 展示常用回复
     */
    var autoReplyHandler = function(_replys){
    	var id = $("#hid_current_visitId").val();
    	var $replyObj = eval("(" + _replys + ")");
    	$("#replyArea_"+id).empty();
    	 $.each($replyObj,function(i,replyObj){
    		 var replyId = replyObj.id;
    		 $("#replyArea_"+id).append($.format(replyP,replyObj.title,replyObj.content,replyId));
    		 $(document).off(".rly_"+replyId);
    		 $(document).on('click.rly_'+replyId,'#'+replyId,replyClick);
    		 //$("#"+replyId).live('click',replyClick);
     	 });  
    };
    
    /**
     * 重复登录处理-[leaveInfo]
     */
    var leaveInfoHandler = function(_leaveInfoMsg){
    	output("<span style=\"color:red\">"+_leaveInfoMsg+"</span>", "lvEdit", "系统");
    	var targetId = $("#hid_current_visitId").val();
    	output("<span style=\"color:red\">"+_leaveInfoMsg+"</span>", targetId, "系统");
    	$("#visitInfo").empty();
    	$("#visitlist").empty();
    	$("#seatlist").empty();
    	$("#seatConnectlist").empty();
    	
    };
    
    /**
     * 添加用户信息至坐席列表
     */
    var addUserToSeat = function(userVo,_status){
    	var $userLi = $('li.li_id_show_' + userVo.id);
    	if($userLi.length>0){
    		return;
    	}
    	$("#seatlist").append($.format(seatLi,userVo.id,userVo.username,userVo.dept));
    };
  /**
   * 转接人员
   */
    var adapterVisit =  function(){
    	$this = $(this);
    	var _visitId = $this.attr("userid");
    	hallSocket.emit('adapterVisit',_visitId,userId);
    };
    /**
     * 添加用户信息至营业员列表
     */
    var addUserToVisit = function(userVo,_status){
    	var $userLi = $('li.userChatItems_' + userVo.id);
    	if($userLi.length>0){
    		return;
    	}
    	 $("#visitlist").append($.format(visitLi,userVo.id,userVo.username,userVo.dept));
    	 //显示对话div
     	 var $templateContent = $('#chatTemplate');
         var data = [
             {uid: userVo.id}
         ];
         $templateContent.tmpl(data).appendTo($('#chatContainers'));
         var _visitId = userVo.id;
         //转接按钮
         var $adapterUserLi = $("li.li_id_click_adapter_"+_visitId);
         
         $(document).off(".adapter_"+_visitId);
 		$(document).on('click.adapter_'+_visitId,'.li_id_click_adapter_'+_visitId,adapterVisit);
    };
    /**
     * 添加用户信息至营业员活动
     */
    var addUserToConnectVisit = function(_userId,_userName,_status){
    	var $userLi = $('li.li_id_click_' + _userId);
    	if($userLi.length>0){
    		return;
    	}
    	 $("#seatConnectlist").append($.format(connectVisitLi,_userId,_userName));
    };
    
    /**
     * 首次进入监听，获取所有在线用户-[userList]
     */
    var userListHandler = function(_ulstMap){
    	var ulstObj = eval("(" + _ulstMap + ")");
    	$("#seatlist").empty();
    	 $.each(ulstObj,function(key,values){
    		var userVo = ulstObj[key];
 		    if(userVo.jobCode==="1"){
 		    	addUserToSeat(userVo,onlineCode);
 		    }else if(userVo.jobCode==="0"){
 		    	addUserToVisit(userVo,onlineCode);
 		    };
    	 });  
    };
    
    /**
     * 坐席已在线，当有新用户进入时，刷新列表-[addUserToList]
     */
    var addUserToListHandler = function(_userVo){
    	var userVo = eval("(" + _userVo + ")");
    	if(userVo.jobCode==="1"){
    		addUserToSeat(userVo,onlineCode);
    	}else if(userVo.jobCode==="0"){
    		addUserToVisit(userVo,onlineCode);
    	}
    };
    /**
     * 显示更新同时在线人数
     */
    var userCountHandler = function(_count){
    	$("#userCount").html("当前在线人数：" + _count);
    };
    
    /**
     * 坐席离线，刷新用户列表-[removeUserToList]
     */
    var removeUserToListHandler = function(_userVo){
    	var userVo = eval("(" + _userVo + ")");
    	var _userId = userVo.id;
    	var $userLi = $('li.userChatItems_' + _userId);
    	$userLi.remove();
    	var $chatContainer = $(".chatContainer_" + _userId);
    	$chatContainer.remove();
    	var $defaultContainer = $("#defaultChatContainer");
    	$defaultContainer.show();
    	//hallSocket.emit("userIsLeave",_userId);
    	
    };
    
    /**
     * 初始化UE
     */
    function initUE(id) {
    	var _content;
    	$("textarea[name='"+(UEDITOR_CONFIG.textarea?UEDITOR_CONFIG.textarea:'editorValue')+"']").remove();
    	var ue1;
    	if(id){
    		ue1 = UE.getEditor('editor_'+id);
    	}else {
			ue1 = UE.getEditor('editor');
		}
    	
    	if(ue1){
    		_content = ue1.getContent();
    		ue1.destroy();
    	}
    	var $bgArea = $(".l_service_bj_"+id);
    	$bgArea.empty();
    	var $editor;
    	if(id){
    		$editor = $("<div id = 'editor_"+id+"'></div>");
    	}else{
    		$editor = $("<div id = 'editor'></div>");
    	}
    	var _w;
    	if(id){
    		_w = $(".l_service_bj_"+id).width();
    	}else{
    		_w = $(".l_service_bj").width();
    	}
    	//console.log(_w-1);
		$editor.css({
			width:(_w-1),
			height:167
		});
		$bgArea.append($editor);
		if(id){
			setTimeout(function(){
				var ue = UE.getEditor('editor_'+id,{ 
					initialFrameWidth : (_w-1),
			        initialFrameHeight: 167,
			        basePath:basePath
				});
				ue.ready(function(){
					if(_content){
						ue.setContent(_content);
					}
				});
				
			},10);
		}else{
			setTimeout(function(){
				var ue = UE.getEditor('editor',{ 
					initialFrameWidth : (_w-1),
			        initialFrameHeight: 167,
			        basePath:basePath
				});
				ue.ready(function(){
					if(_content){
						ue.setContent(_content);
					}
				});
				
			},10);
		}
	
		
    }
    
    /**
     * 获取评价链接
     */
    var sendEvaluation = function(){
    	var _visitId = $(this).attr("userid");
    	hallSocket.emit('scoreInfo',_visitId,userId);
    };
    
    /**
     * 点击营业员列表事件
     */
    var visitLiClick = function(){
    	var $this = $(this);
    	var _visitId = $this.attr("userid");
    	$("#hid_current_visitId").val(_visitId);
    	var $visitLi = $("li.li_id_click_" + _visitId);
    	//删除其他li的selected样式
    	$('.userChatLi').removeClass("selected");
    	$visitLi.removeClass("msgFlash1");
    	$visitLi.removeClass("msgFlash2");
    	$visitLi.addClass("selected");
    	$visitLi.addClass("conecting");
    	//$('#chatContainers').empty();
    	$(".chatContainer").hide();
    	//显示对话div
    	/*var $templateContent = $('#chatTemplate');
        var data = [
            {uid: _visitId}
        ];
        $templateContent.tmpl(data).appendTo($('#chatContainers'));*/
        var $chatContainer = $(".chatContainer_" + _visitId);
    	
    	$chatContainer.show();
    	hallSocket.emit("autoReply");
    	//评价
    	$(".evaluation_"+_visitId).attr("userid",_visitId);
    	
    	/**
		 * 发送评价信息
		 */
    	$(document).off(".eval_"+_visitId);
		$(document).on('click.eval_'+_visitId,'.evaluation_'+_visitId,sendEvaluation);
		//$(".evaluation_"+_visitId).live('click',sendEvaluation);
        //initUE(_visitId);
    	$(window).resize();
    };
    
    //常用回复
    var ksButtonClick = function(){
    	$this = $(this);
    	var id = $this.attr("seatid");
    	$(".hfls_"+id).toggle();
    };
    /**
     * 坐席应答处理
     */
    var responseConnectClick = function(){
    	var $this = $(this);
    	var _visitId = $this.attr("userid");//删除其他li的selected样式
    	var _userName = $this.find("a").text();
    	addUserToConnectVisit(_visitId, _userName, onlineCode);
    	$('.userChatLi').removeClass("selected");
    	//当前聊天窗口visitId
    	$("#hid_current_visitId").val(_visitId);
    	hallSocket.emit('seatResponseVisit',userId,_visitId);
    	var $visitLi = $("li.li_id_show_"+_visitId);
    	var $visitConnectLi = $("li.li_id_click_"+_visitId);
    	var $seatFinalityVisit = $("li.li_id_click_finality_"+_visitId);
    	//取消请求连接闪烁
    	$visitLi.removeClass("visitIsIn");
    	$visitLi.removeClass("msgFlash1");
    	$visitLi.removeClass("msgFlash2");
    	//设置状态为当前会话
    	$visitConnectLi.addClass("selected");
    	$visitConnectLi.addClass("conecting");
    	
    	//$('#chatContainers').empty();
    	$(".chatContainer").hide();
    	/*//显示对话div
    	var $templateContent = $('#chatTemplate');
        var data = [
            {uid: _visitId}
        ];
        $templateContent.tmpl(data).appendTo($('#chatContainers'));*/
        var $chatContainer = $(".chatContainer_" + _visitId);
    	
    	$chatContainer.show();
    	 $(document).off(".send_"+_visitId);
		 $(document).on('click.send_'+_visitId,'.l_service_cz_button_'+_visitId,sendMessageHandler);
    	//更新titleInfo
    	$titleInfo = $("#titleInfo_"+_visitId);
    	$titleInfo.empty();
    	var $titleInfoTemplate = $("#titleInfoTemplate");
    	var titleInfoText = [
    	    {username:_userName}
    	];
    	$titleInfoTemplate.tmpl(titleInfoText).appendTo($titleInfo);
    	hallSocket.emit("autoReply");
    	//评价
    	$(".evaluation_"+_visitId).attr("userid",_visitId);
    	/**
		 * 发送评价信息
		 */
		//$(".evaluation_"+_visitId).live('click',sendEvaluation);
    	$(document).off(".eval_"+_visitId);
		$(document).on('click.eval_'+_visitId,'.evaluation_'+_visitId,sendEvaluation);
       // initUE(_visitId);
    	$(window).resize();
    	//应答后，附上常规点击
    	$visitConnectLi.on("click",visitLiClick);
    	$seatFinalityVisit.on("click",seatFinalityVisit);
    	var $ksButton =  $(".ksButton_"+_visitId);
    	$ksButton.on('click',ksButtonClick);
    	
    	$(document).off(".his_btn_"+_visitId);
		$(document).on('click.his_btn_'+_visitId,'#his_btn_'+_visitId,function(){
			if(!$("#his_date_"+_visitId).val()){
				alert("请选择日期");
				return;
			}
			
			hallSocket.emit('historyInfoShow',userId,_visitId,$("#his_date_"+_visitId).val());
		});
    	
    };
    //客服人员主动，中断会话，强制营业员退出
    var seatFinalityVisit = function(){
    	var $this = $(this);
    	var _visitId = $this.attr("userid");
    	hallSocket.emit('seatFinalityVisit',_visitId);
    }
    
    /**
     * 营业员请求连接-[visitRequest]
     */
    var visitRequestHandler = function(_visitId){
    	var $visitLi = $('li.li_id_show_' + _visitId);
    	$visitLi.addClass(visitIsIn);
    	$visitLi.one('click',responseConnectClick);
    	
    };
    
    /**
     * 坐席页面展示营业员信息
     */
    var visitInfoHandler = function(_visitInfo){
    	var $visitUserObj = eval("(" + _visitInfo + ")");
    	$visitInfo = $("#visitInfo");
		$visitInfo.empty();
		var $visitTemplate = $("#visitTemplate");
		$visitTemplate.tmpl($visitUserObj).appendTo($visitInfo);
    };
    
    /**
     * 发送消息-[sendMessage]
     */
    window.sendMessageHandler = sendMessageHandler = function(){
    	var _visitId = $("#hid_current_visitId").val();
    	var visitUE = UE.getEditor('editor_'+_visitId);
    	
    	var message = visitUE.getContent();
    	if(!message){
    		alert("请不要发送空消息！");
    		return;
    	}
    	var jsonObject = {'@class': 'com.zx.chatroom.vo.Chat',
    			fromUserId : userId, 
    			fromUserName : "", 
				toUserId : _visitId,
				toUserName : "",
				message : message};
		hallSocket.json.send(jsonObject);
		//清除消息闪烁
    	$.blinkTitle.clear();
    };
    
    /**
     * 监听接收消息，所有监听到的消息，都是发给自己的-[receiveMessage]
     */
    var receiveMessageHandler = function(_message){
    	var chatObj = eval("(" + _message + ")");
    	var message = chatObj.message;
    	if(chatObj.fromUserId === userId){
    		output(message, chatObj.toUserId, chatObj.fromUserName);
    		var ue = UE.getEditor('editor_'+chatObj.toUserId);
        	ue.setContent("");
    	}else if(chatObj.toUserId === userId){
    		var _visitId = chatObj.fromUserId;
    		//收到消息消息列表闪烁
    		var $visitLi = $("li.li_id_click_" + _visitId);
    		$visitLi.addClass(visitIsIn);
    		output(message, chatObj.fromUserId, chatObj.fromUserName);
    		var ue = UE.getEditor('editor_'+chatObj.fromUserId);
        	ue.setContent("");
        	//新消息闪烁
        	if (CurrentActive) {
        		//先清理，防止重复操作
        		$.blinkTitle.clear();
        		$.blinkTitle.show();
			}
    	}
    };
    
    /**
     * 显示评价信息
     */
    var scoreInfoHandler = function(_scoreInfo){
    	var scoreObj = eval("(" + _scoreInfo + ")");
    	var vUser = eval("(" + scoreObj.vId + ")");
    	output(scoreObj.info,vUser.id,userName);
    };
    
    /**
     * 营业员离开，刷新坐席列表-[visitIsLeave]
     */
    var visitLeaveHandler = function(_info){
    	var vUser = eval("(" + _info + ")");
    	var _visitId = vUser.id;
    	var vUserName = vUser.username;
    	var $leaveVisitLi = $('li.userChatItems_' + _visitId);
    	var $leaveClickVisitLi = $("li.li_id_click_" + _visitId);
    	if($leaveVisitLi){
    		$leaveVisitLi.removeClass("visitIsIn");
    		$leaveVisitLi.removeClass("msgFlash1");
    		$leaveVisitLi.removeClass("msgFlash2");
    		$leaveVisitLi.removeClass("conecting");
    		$leaveVisitLi.removeClass("selected");
    		var $chatContainer = $(".chatContainer_" + _visitId);
    		$chatContainer.hide();
    		output(vUserName+"离开","lvEdit","系统");
    		//connect列表刷新
    		$leaveClickVisitLi.remove();
    		var $defaultContainer = $("#defaultChatContainer");
        	$defaultContainer.show();
    	}
    };
    
    /**
     * 打印评价成功消息
     */
    var evalSuccessHandler = function(_msgInfo){
    	var msgObj = eval("(" + _msgInfo + ")");
    	var vUser = eval("(" + msgObj.vId + ")");
    	output(msgObj.info,"lvEdit",vUser.username);
    };
    
    /**
     * resize的时候，重新初始化编辑器
     */
    $(window).resize(function(){
    	var $hidVisitId =  $("#hid_current_visitId");
    	//if($hidVisitId.val()){
    		initUE($hidVisitId.val());
    	//}
    });
    /**
     * 显示截图
     */
    var snapscreenPathHandler = function(jsonInfo){
    	var snapObj = eval("(" + jsonInfo + ")");
    	var snapscreenPath = snapObj.imgSrc;
    	var userName = snapObj.fromUserName;
    	var targetId = $("#hid_current_visitId").val();
    	var ue = UE.getEditor('editor_'+targetId);
    	ue.setContent(snapscreenPath);
    	//output(snapscreenPath,targetId,userName);
    };
    
    var userLeaveMessageHandler = function(chatJson){
    	var chatObj = eval("(" + chatJson + ")");
    	//alert(chatObj.message);
    	output(chatObj.message,"lvEdit","系统");
    };
    /**
     * 连接转接用户
     */
    var adapterUserHandler =  function(visitUserVoJson){
    	var visitUserVo = eval("(" + visitUserVoJson + ")");
    	var _visitId = visitUserVo.id;
    	var _userName = visitUserVo.username;
    	addUserToConnectVisit(_visitId, _userName, onlineCode);
    	$('.userChatLi').removeClass("selected");
    	//当前聊天窗口visitId
    	$("#hid_current_visitId").val(_visitId);
    	hallSocket.emit('seatResponseVisit',userId,_visitId);
    	var $visitLi = $("li.li_id_show_"+_visitId);
    	var $visitConnectLi = $("li.li_id_click_"+_visitId);
    	var $seatFinalityVisit = $("li.li_id_click_finality_"+_visitId);
    	//取消请求连接闪烁
    	$visitLi.removeClass("visitIsIn");
    	$visitLi.removeClass("msgFlash1");
    	$visitLi.removeClass("msgFlash2");
    	//设置状态为当前会话
    	$visitConnectLi.addClass("selected");
    	$visitConnectLi.addClass("conecting");
    	
    	//$('#chatContainers').empty();
    	$(".chatContainer").hide();
    	/*//显示对话div
    	var $templateContent = $('#chatTemplate');
        var data = [
            {uid: _visitId}
        ];
        $templateContent.tmpl(data).appendTo($('#chatContainers'));*/
        var $chatContainer = $(".chatContainer_" + _visitId);
    	
    	$chatContainer.show();
    	 $(document).off(".send_"+_visitId);
		 $(document).on('click.send_'+_visitId,'.l_service_cz_button_'+_visitId,sendMessageHandler);
    	//更新titleInfo
    	$titleInfo = $("#titleInfo_"+_visitId);
    	$titleInfo.empty();
    	var $titleInfoTemplate = $("#titleInfoTemplate");
    	var titleInfoText = [
    	    {username:_userName}
    	];
    	$titleInfoTemplate.tmpl(titleInfoText).appendTo($titleInfo);
    	hallSocket.emit("autoReply");
    	//评价
    	$(".evaluation_"+_visitId).attr("userid",_visitId);
    	/**
		 * 发送评价信息
		 */
		//$(".evaluation_"+_visitId).live('click',sendEvaluation);
    	$(document).off(".eval_"+_visitId);
		$(document).on('click.eval_'+_visitId,'.evaluation_'+_visitId,sendEvaluation);
       // initUE(_visitId);
    	$(window).resize();
    	//应答后，附上常规点击
    	$visitConnectLi.on("click",visitLiClick);
    	$seatFinalityVisit.on("click",seatFinalityVisit);
    	var $ksButton =  $(".ksButton_"+_visitId);
    	$ksButton.on('click',ksButtonClick);
    	
    	$(document).off(".his_btn_"+_visitId);
		$(document).on('click.his_btn_'+_visitId,'#his_btn_'+_visitId,function(){
			if(!$("#his_date_"+_visitId).val()){
				alert("请选择日期");
				return;
			}
			
			hallSocket.emit('historyInfoShow',userId,_visitId,$("#his_date_"+_visitId).val());
		});
    };
    /**
     * 用户被转接，移除
     */
    var removeUserLiHandler = function(expiredUserId){
    	var $expiredUserLi =  $('li.li_id_click_' + expiredUserId);
    	$expiredUserLi.remove();
    }
    	
		userInfoObj = eval("(" + $("#hid_userInfo").val() + ")");
		userId = userInfoObj.id;
		userName = userInfoObj.username;
		$("#jumpLink").append("<a href=\"talk?baseId="+userId+"\" target=\"_blank\">进入群组</a>");
		/**
		 * 截图插件
		 */
		
			function plugin0()
		    {
		        return document.getElementById('plugin0');
		    }
			
		    plugin = plugin0;
		    
		    function snapshot(){
		    	 if(undefined==plugin().snapshot){
					 $("#dialog_info").show();
				 }else{
					 var visitId = $("#hid_current_visitId").val();
				    	if(visitId){
				    		plugin().snapshot($("#localAddr").val(),"/chatroomV2/snapscreenUp/"+userId+"/"+visitId+"/hall",$("#localPort").val());
				    	}
				 }
		    }
		    $(document).off(".snapshotButtonClick");
		    $(document).on("click.snapshotButtonClick",".snapshotButton",snapshot);
		    
		    $(document).off(".dialogCloseClick");
		    $(document).on("click.dialogCloseClick",".t_pic",function(){
		    	$("#dialog_info").hide();
		    });
		
		
		
		
		/**
		 * 页面加载完成，连接大厅
		 */
		hallSocket.on('connect', connectHandler);
		/**
		 * 加载常用回复
		 */
		hallSocket.on('autoReply',autoReplyHandler);
		/**
		 * 监听重复登录
		 */
		hallSocket.on('leaveInfo',leaveInfoHandler);
		/**
		 * 首次进入监听，获取所有在线用户
		 */
		hallSocket.on('userList',userListHandler);
		/**
		 * 坐席已在线，当有新用户进入时，刷新列表
		 */
		hallSocket.on('addUserToList',addUserToListHandler);
		/**
		 * 监听同时在线人数变化-[addUserToList]
		 */
		hallSocket.on('userCount',userCountHandler);
		/**
		 * 坐席离线
		 */
		hallSocket.on('removeUserToList',removeUserToListHandler);
		/**
		 * 营业员请求监听
		 */
		hallSocket.on('visitRequest',visitRequestHandler);
		/**
		 * 坐席应答营业员，获取营业员信息
		 */
		hallSocket.on('visitInfo',visitInfoHandler);
		/**
		 * 接收消息
		 */
		hallSocket.on('receiveMessage',receiveMessageHandler);
		/**
		 * 监听营业员离开
		 */
		hallSocket.on('visitLeave',visitLeaveHandler);
		/**
		 * 监听评价
		 */
		hallSocket.on('scoreInfo',scoreInfoHandler);
		/**
		 * 打印评价成功消息
		 */
		hallSocket.on('evalSuccess',evalSuccessHandler);
		/**
		 * 用户离开，打印离开消息
		 */
		hallSocket.on('userLeaveMessage',userLeaveMessageHandler);
		/**
		 * 上传截图
		 */
		hallSocket.on("snapscreenPath",snapscreenPathHandler);
		/**
		 * 转接用户
		 */
		hallSocket.on("adapterUser",adapterUserHandler);
		/**
		 * 转接移除过期用户
		 */
		hallSocket.on("removeUserLi",removeUserLiHandler);
		/**
		 * 显示历史记录
		 */
		hallSocket.on('hisInfo',function(json){
			//放到隐藏div里
			var _visitId = $("#hid_current_visitId").val();
			$("#lvEdit_"+_visitId).removeClass("l_serviceHidden");
			
			var $closeBtn = $(".serviceList_close_btn_"+_visitId);
			$("#lvEdit_"+_visitId).html("");
			$("#lvEdit_"+_visitId).html("<span style ='color:red'>历史记录</span><br/>"+json);
			$closeBtn.removeClass("serviceList_close_btn");
			$closeBtn.addClass("serviceList_close_btn_show");
			$("#lvEdit_"+_visitId).show();
			$closeBtn.show();
			$(document).off(".closeBtn_"+_visitId);
			$(document).on('click.closeBtn_'+_visitId,'.serviceList_close_btn_'+_visitId,function(){
				$("#lvEdit_"+_visitId).hide();
				$closeBtn.hide();
			});
			
		});
	
	});
	
	/**
	 * 聊天窗口div自动滚动至底部
	 */
	function scollToButtom(elementId) {
        $("#" + elementId).animate({
            scrollTop: $("#" + elementId)[0].scrollHeight
        }, 1000);
    }
	
	/**
	 * @param _message 输出信息
	 * @param _targetId 输出信息打印区域元素ID
	 * @param _userName 信息输出者NAME
	 */
	var output = function(_message, _targetId, _userName) {
        var currentTime = "<span class='time'>" + moment().format('HH:mm:ss') + "</span>";
        var element = $("<div>" + currentTime + " " + _message + "</div>");
        if (!_targetId) {
        	_targetId = "lsEdit";
        } else {
            element = $("<div>" + currentTime + " " + _userName + "：" + _message + "</div>");
        }
        $("#" + _targetId).append(element);
        scollToButtom(_targetId);
    };
	
}).call(this);

/**
 * 营业员列表li[忙碌busy]
 */
var visitLiBusy = "<li userid='{0}' class='userChatLi disClick isBusy userChatItems_{0} li_id_{0}'><a class='linkChat' userid='{0}' href='#'><span></span>{1}</a></li>";

/**
 * 营业员列表li
 */
var visitLi = "<li userid='{0}' class='userChatLi userChatItems_{0} li_id_show_{0}' title='{2}'><a class='linkChat' userid='{0}' href='#'><span></span>{1}({2})</a></li>"+
"<li userid='{0}' class='L_xuangxiangka_zj li_id_click_adapter_{0}' title='转接会话'></a></li>";
/**
 * 当前连接营业员列表
 */
var connectVisitLi = "<li userid='{0}' class='userChatLi userChatItems_{0} li_id_click_{0}' title='{1}'><a class='linkChat' userid='{0}' href='#'>{1}</a></li>" +
		"<li userid='{0}' class='L_xuangxiangka_close li_id_click_finality_{0}' title='中断会话'></a></li>";
/**
 * 坐席列表li
 */
var seatLi = "<li userid='{0}' class='userChatLi userChatItems_{0} li_id_{0}' title='{2}'><span class='linkChat' userid='{0}'>{1}({2})</span></li>";
/**
 * 常用回复段落P
 */
var replyP = "<p id = '{2}' title = '{1}'>{0}</p>";